package com.dkm.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import com.dkm.entity.Users;
import com.dkm.service.UsersService;

/**
 * 自定义密码匹配器
 * @author 狄凯明
 *
 */

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

	
	@Override
    public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
        System.out.println("密码匹配器启用===》");
    	UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authcToken;
    	//获取用户名、密码
    	String login_name=usernamePasswordToken.getUsername();
    	//char[] password=;
    	
        //数据库存储密码
        Object accountCredentials = getCredentials(info);

        //使用MD5对前台的密码加密并加盐
		//定义盐值
		//(ByteSource)impor package:org.apache.shiro.util
		Object salt=ByteSource.Util.bytes(login_name);
		int hashIterations=1024;//加密次数
		Object encryptionStr = new SimpleHash("MD5",usernamePasswordToken.getPassword(),salt,hashIterations);
		
		System.out.println("encryptionStr>="+encryptionStr);
        
        System.out.println("前台输入密码=》"+String.valueOf(usernamePasswordToken.getPassword()));
        System.out.println("加密后密码=》"+encryptionStr);
        System.out.println("数据库密码=》"+accountCredentials);
        
        //将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
        
        if(equals(String.valueOf(encryptionStr), accountCredentials)){
        	System.out.println("equals");
        }else {
        	System.out.println("not equals");
        }
        return equals(String.valueOf(encryptionStr), accountCredentials);
    }
}
